﻿//-----------------------------------------------------------------------
// <copyright file="Program.cs" company="CodePlex">
//     Copyright (c) Doug Holland, CodePlex. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace CodePlex.Diagnostics.Profiler.Console
{
    using System;

    using System.Diagnostics;

    using System.Globalization;

    using CodePlex.Diagnostics.Services;

    /// <summary>
    /// Provides a console based application to enable profiling of the CodePlex.Diagnostics framework.
    /// </summary>
    internal static class Program
    {
        /// <summary>
        /// Program entry point.
        /// </summary>
        private static void Main()
        {
            AppDomain.CurrentDomain.UnhandledException += delegate(object sender, UnhandledExceptionEventArgs e)
                                                          {
                                                              Exception ex = e.ExceptionObject as Exception;

                                                              Trace.WriteLine(ex.Message);
                                                              Trace.WriteLine(ex.StackTrace);
                                                          };

            // start the diagnostics service.
            DiagnosticsServiceHost.StartService();

            try
            {
                // force a divide by zero exception.
                for (int index = 0; index < 10; index++)
                {
                    index /= index;
                }
            }
            catch (DivideByZeroException ex)
            {
                DateTime start = DateTime.Now;

                Guid publicationId = ex.Publish();

                // measure the duration of the call to publish the exception.
                TimeSpan duration = DateTime.Now - start;

                Console.WriteLine(string.Format(CultureInfo.CurrentCulture, "Publication Id: {0}", publicationId));

                Console.WriteLine(string.Format(CultureInfo.CurrentCulture, "Call Duration: {0}", duration));
            }

            // stop the diagnostics service.
            DiagnosticsServiceHost.StopService();
        }
    }
}
